package com.cneiu.ppms.web.service;

import java.util.Map;

import net.sf.json.JSONObject;

import com.cneiu.ppms.web.base.BaseService;
import com.cneiu.ppms.web.model.RoleGroupModel;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
/**
 * 角色分组表业务
 * @author jiangli
 * @date 2013-04-20
 */
public class RoleGroupService extends BaseService{
	
	/**
	 * 分页列出角色分组表
	 * @param page
	 * @param limit
	 * @param search
	 * @return
	 */
	public static Page<RoleGroupModel> list(Map<String,String> params){
		JSONObject search = JSONObject.fromObject(params.get("search"));
		int limit = 0;
		int page = 1;
		try{
			limit = params.get("limit")==null?10:Integer.parseInt(params.get("limit").toString());
			page = params.get("page")==null?1:Integer.parseInt(params.get("page").toString());
		}catch (Exception e){
			limit = 10;
			page = 1;
		}
		StringBuffer where = new StringBuffer();;
		try{
			if (search.get("group_name") != null){
				where.append(" AND GROUP_NAME=" + search.get("group_name"));
			}
		}catch (Exception e){
		}
		Page<RoleGroupModel> actions = null;
		
		String select_sql = "SELECT * ";
		String query_sql = "FROM SA_ROLE_GROUP WHERE 1=1 " + where;
		try{
			actions = RoleGroupModel.dao.paginate(page, limit, select_sql, query_sql);
		}catch (Exception e){
			setError(e.getMessage());
		}
		return actions;
	}

	/**
	 * 添加角色分组表
	 * @param params
	 * @return
	 */
	public static boolean isAdd(Map<String,String> params){
		Record record = new Record();
		record.set("GROUP_NO", Db.queryBigDecimal("select sa_role_group_seq.nextval from dual").intValue());
		record.set("GROUP_NAME", params.get("group_name").toString());
		boolean isupadded = Db.save("SA_ROLE_GROUP", "GROUP_NO",record);
		if(isupadded){
			return true;
		}
		return setError("保存角色分组信息失败！");
	}

	/**
	 * 根据ID查询角色分组表
	 * @param params
	 * @return
	 */
	public static RoleGroupModel get(Map<String,String> params){
		String no = params.get("group_no")==null?"":params.get("group_no").toString();
		RoleGroupModel pp = RoleGroupModel.dao.findFirst("SELECT * FROM SA_ROLE_GROUP WHERE GROUP_NO=?", no);
		return pp;
	}

	
	/**
	 * 修改角色分组表
	 * @param params
	 * @return
	 */
	public static boolean isUpdate(Map<String,String> params){
		String act_no = params.get("group_no").toString();
		Record record = new Record();
		record.set("GROUP_NO", act_no);
		record.set("GROUP_NAME", params.get("group_name").toString());
		boolean isupdated = Db.update("SA_ROLE_GROUP", "GROUP_NO",record);
		if(isupdated){
			return true;
		}
		return setError("您访问的角色分组信息不存在！");
	}

	/**
	 * 删除角色分组表
	 * @param params
	 * @return
	 */
	public static boolean isDelete(Map<String,String> params){
		String no = params.get("group_no")==null?"":params.get("group_no").toString();
		boolean deleted = Db.deleteById("SA_ROLE_GROUP", "GROUP_NO", no);
		if (deleted){
			return true;
		}
		return setError("您访问的用角色分组信息不存在！");
	}
}
